<?php 
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi
 
    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

require_once(dirname(__FILE__).'/../config/ProjectConfiguration.class.php');

$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', true);
new sfDatabaseManager($configuration);
//mailer to send a message to users that have a task that are due today

//grab all tasks that are due today
$date = date("Y-m-d", time());
$weekagodate = time() - (7* 24 * 60 * 60);
$sendweekly = false;
if(date("D",time()) == "Sun") {
	$sendweekly = true;
}
$tasks = TaskPeer::retrieveLateTasksByDate($date);
$count = 0;

//for each late task
foreach($tasks as $index=>$task)
{	
	$taskdate = $task['due_date'];
	$dateArr  = explode("-",$taskdate);
	$convertedtdate = mktime(0,0,0,$dateArr[1],$dateArr[2],$dateArr[0]) ;
	if(!$sendweekly && $convertedtdate <  $weekagodate) {
		continue;
	}

	$count = $count + 1;
	
	//retrieve data
	$user_id = $task['user_id'];
	$user = UserPeer::retrieveById($user_id);
	$name = $task['name'];
	$group_id = $task['group_id'];
	$group = GroupPeer::retrieveById($group_id);
	$group_name = $group->getName();
	$email = $user->getEmail();

	
	//create message

	if($taskdate == $date) {
		$subject = 'Task Due Today';
	
		$body = 'You have a Task that is Due Today. <br/>';
		$body = $body.'Group Name: '.$group_name.'<br/>';
		$body = $body.'Task Name: '.$name.'<br/>';
		$body = $body.'Due Date: '.$date.'<br/>';
		$body = $body.'<a href="http://www.finalsclub.org/groups/show/id/'.$group->getId().'"> view the task.</a>';
		$body = $body.'<hr/> FinalsClub copyright 2008';

		$plainbody = 'You have a task "'.$name.'" for group "'.$group_name.'" that is due today at finalsclub.org.';
	} else {
		$subject = 'Reminder: Late Task';
	
		$body = 'You have a Task that is OverDue. <br/>';
		$body = $body.'Group Name: '.$group_name.'<br/>';
		$body = $body.'Task Name: '.$name.'<br/>';
		$body = $body.'Due Date: '.$date.'<br/>';
		$body = $body.'<a href="http://www.finalsclub.org/groups/show/id/'.$group->getId().'"> view the task.</a>';
		$body = $body.'<hr/> FinalsClub copyright 2008';

		$plainbody = 'You have an overdue task "'.$name.'" for group "'.$group_name.'" that was due on '.$taskdate.' at finalsclub.org.';
	}

	mailerclass::mail($email, $subject, $body, $plainbody);
}

echo('mailed '.$count.' message(s).');


class mailerclass {
	public static function mail($email, $subject, $body, $plainbody) {
		try {
			// Create the mailer
			$smtp_server = "smtp.gmail.com"; // sfConfig::get('app_smtp_server');  
			$smtp_user = "support@finalsclub.org"; // sfConfig::get('app_smtp_user');
			$smtp_password = "f1nal5club"; // sfConfig::get('app_smtp_password');

		  	$connection = new Swift_Connection_SMTP($smtp_server, 465, Swift_Connection_SMTP::ENC_SSL);
			$connection->setUsername($smtp_user);
			$connection->setPassword($smtp_password);
			$mailer = new Swift($connection);  
	
			$message = new Swift_Message($subject);
			$message->attach(new Swift_Message_Part($body, 'text/html'));	
			$message->attach(new Swift_Message_Part($plainbody, 'text/plain'));
		
			$mailer->send($message, $email, $smtp_user);
			$mailer->disconnect();
		} catch (Exception $e) {
			echo $e->getMessage();
		}
	}
}

?>
